function result = file_read(src_name, dst_name)
%   pc_bee4_ddr3_rw -- Download and upload data between PC and BEE4
%   using pc_bee4_ddr3_rw.mdl
%
%   This function demonstrates how to upload data to the
%   on-FPGA Block RAM, store data to DDR3 memory, transfer data back to
%   another on-FPGA Block RAM, and download from the on-FPGA Block RAM.
%
%   Example using the function is:
%     abc = 1:1024*1024/4;
%     close all;
%     fid = fopen('file.dat','wb');
%     fwrite(fid,abc,'uint32');
%     close all;
%     result = pc_bee4_ddr3_rw('192.168.0.100', 'A','file.dat',0);
%
%   The arguments to the function are the address/hostname of the BEE4
%   system, ID of the FPGA running the FMC101 reference design, file name
%   to be upload to FPGA and data length, respectively.
%   Valid FPGA IDs are 'A', 'B', 'C', or 'D'.
%   length of the data is represented by KBytes. eg. 1 means upload
%   1KBytes. 0 means upload the whole file.
%   Please note that there may not be any serial port terminals open on
%   the BEE4 system, or else the underlying hardware operations will fail.

switch nargin
    case 2
        if ~ischar(dst_name),error('file name must be a string');end
        if ~ischar(src_name),error('file name must be a string');end        
    otherwise
        error('Wrong number of input arguments');
end

src_fid = fopen(src_name,'rb');
dst_fid = fopen(dst_name, 'wb');
if src_fid == -1 | dst_fid == -1
    error('File can not be opened');
end

%% main write process
%% read data

data_length = 32;
index = 2
result = data_length ;
fseek(src_fid, index, 'bof');
data = fread(src_fid, data_length, 'double', index);
fwrite(dst_fid, data, 'double');
fprintf('read file done !\n');
fclose all;

end

